package com.blogs.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.blogs.component.BaseActivity;
import com.blogs.service.FeedBackTask;
import com.blogs.service.FeedBackTask.IMsgCallBack;
import com.blogs.tools.DBSharedPreferences;
import com.cnblogs.R;
import com.cnblogs.R.color;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FeedBackActivity extends BaseActivity implements OnClickListener{
	private int maxLength=140;
	private DBSharedPreferences sp;
	private TextView title_include_title;
	private TextView feedback_cut_tv;
	private Button title_include_back;
	private Button feedback_send_btn;
	private EditText feedback_content_et;
	private LinearLayout feedback_cut_ll;
	private EditText feedback_email_et;
	private int selectionStart;
	private int selectionEnd;
	private Toast feedToast;
	private ProgressDialog dialog;
	private RelativeLayout title_top_bg;
	private RelativeLayout feedback_bg;
	private RelativeLayout content_buttom_ll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_include_title.setText("意见反馈");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(this);
		feedback_bg=(RelativeLayout)findViewById(R.id.feedback_bg);
		content_buttom_ll=(RelativeLayout)findViewById(R.id.content_buttom_ll);
		feedback_cut_ll=(LinearLayout)findViewById(R.id.feedback_cut_ll);
		feedback_cut_ll.setOnClickListener(this);
		feedback_cut_tv=(TextView)findViewById(R.id.feedback_cut_tv);
		feedback_cut_tv.setText(maxLength+"");
		feedback_content_et=(EditText)findViewById(R.id.feedback_content_et);
		feedback_content_et.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				feedback_cut_tv.setText((maxLength-feedback_content_et.length())+"");
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = feedback_content_et.getSelectionStart();
				selectionEnd = feedback_content_et.getSelectionEnd();
				if(maxLength-feedback_content_et.length()<0)
				{
					s.delete(selectionStart - 1, selectionEnd);
					feedback_content_et.setText(s);
				}
			}
		});
		feedback_email_et=(EditText)findViewById(R.id.feedback_email_et);
		feedback_send_btn=(Button)findViewById(R.id.feedback_send_btn);
		feedback_send_btn.setOnClickListener(this);
		
		dialog = new ProgressDialog(this);           
		dialog.setIndeterminate(true);      
		dialog.setMessage("正在反馈中...");  
		this.InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			feedback_bg.getBackground().setLevel(2);
			content_buttom_ll.getBackground().setLevel(2);
			feedback_email_et.getBackground().setLevel(2);
			feedback_content_et.setTextColor(getResources().getColor(color.title_text_night));
			feedback_email_et.setTextColor(getResources().getColor(color.title_text_night));
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			feedback_bg.getBackground().setLevel(1);
			content_buttom_ll.getBackground().setLevel(1);
			feedback_email_et.getBackground().setLevel(1);
			feedback_content_et.setTextColor(getResources().getColor(color.title_text_light));
			feedback_email_et.setTextColor(getResources().getColor(color.title_text_light));
		}
	}
	public void onClick(View v) {
		if(v==title_include_back)
		{
			finish();
		}
		else if(v==feedback_cut_ll)
		{
			feedback_cut_tv.setText(maxLength+"");
			feedback_content_et.setText("");
		}
		else if(v==feedback_send_btn)
		{
			Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
			Matcher m = p.matcher(feedback_email_et.getText().toString().trim());
            if(!m.matches())
            {
            	showToast("亲,你的邮箱输入有误哦");
            	return;
            }
			if(feedback_content_et.length()<6)
			{
				showToast("亲,太简短了吧!");
				return;
			}
			dialog.show();
			String con=feedback_email_et.getText().toString()+":   "+ feedback_content_et.getText().toString();
			new FeedBackTask(con, new IMsgCallBack() {
				
				public void onSuccess() {
					// TODO Auto-generated method stub
					feedback_content_et.setText("");
					feedback_email_et.setText("");
					showToast("信息反馈成功,感谢你的宝贵意见");
					dialog.hide();
				}
				
				public void onFailed() {
					// TODO Auto-generated method stub
					showToast("信息反馈失败");
					dialog.hide();
				}
			}).GetResult();
		}
	}
	private void showToast(String text)
	{
		if(feedToast == null) {  
			feedToast = Toast.makeText(getApplicationContext(), text,
				     Toast.LENGTH_SHORT);
	    } else {  
	    	feedToast.setText(text);       
	    	feedToast.setDuration(Toast.LENGTH_SHORT);  
	    } 
		feedToast.show();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dialog.dismiss();
	}
	
}
